package com.prs.crm.service.stock.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.stock.InventoryDao;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.Inventory;
import com.prs.crm.domain.store.InventoryDetail;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.stock.InventoryService;

public class InventoryServiceImpl extends
		FlowServiceImpl<Inventory, InventoryDao> implements InventoryService {

	@Override
	public void saveInvenTory(Inventory inventory, 
			double[] numberInventorys, File file,	String fileName,
			String[] remarks,ProductStore[] stores) {
		try {
			if(file!=null){
			Attach pAttach = FileUtil.getInstance().saveFile(file,
					fileName, "", Constrains.UPLOAD_DIR_INVENTORY);
				inventory.setInventoryAttach(pAttach.getSrc());}
//			if(file!=null){
//				//File inventoryAttach=new File(file);
//				ServletContext context = (ServletContext) ActionContext.getContext()
//						.get(StrutsStatics.SERVLET_CONTEXT);
//				String path = context.getRealPath("/");
//				ActionContext ctx = ActionContext.getContext();
//			    HttpServletRequest request1 = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
//
//				File dst = new File(path + "wenjian\\" + file.getName());
//				//InetAddress addr = InetAddress.getLocalHost();
//				//String ip=addr.getHostAddress().toString();//获得本机IP
//				inventory.setInventoryAttach(file.getName());
//				try {
//					InputStream in = new BufferedInputStream(new FileInputStream(file), 8192);
//					OutputStream out = new BufferedOutputStream(new FileOutputStream(dst), 8192);
//					int a = 0;
//					byte[] b = new byte[8192];
//					while ((a = in.read(b, 0, 8192)) != -1) {
//						out.write(b, 0, a);
//					}
//					in.close();
//					out.close();
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
			for (int i = 0; i < numberInventorys.length; i++) {
				InventoryDetail denem = new InventoryDetail();
				denem.setName(stores[i].getProduct().getName());
				denem.setInventoryNum(stores[i].getProductNum());
				denem.setBatchNums(stores[i].getBatchNum());
				denem.setProductStore(stores[i]);
				if(stores[i].getProductNum()-numberInventorys[i]>0){
					denem.setInventoryType("盘亏");
				}else if(stores[i].getProductNum()-numberInventorys[i]==0){
					denem.setInventoryType("正常");
				}else{
					denem.setInventoryType("盘盈");
				}
				denem.setNumberInventory(Math.abs(stores[i].getProductNum()-numberInventorys[i]));
				denem.setRemark(remarks[i]);
				inventory.addDetail(denem);
			}
			inventory.setFlowType(0);
			inventory.setInventoryType("0");
			super.save(inventory);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public boolean valideSubmit(Inventory entity) {

		return true;
	}

}
